<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\settle\controller;

use  Mscore\Core\Controller;
use  app\settle\model\Tradeorder as TradeorderModel;
use  app\settle\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class TradeorderController extends Controller{

    private $model;
    private $login;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new TradeorderModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            sleep(2);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){
        $agent=(new CommonModel())->getAccountlist('grade="agent"','aid,username');
        $this->assign('agent', $agent);
        $this->display('list.html'); 
    }

    /**
     * 全部
     * @param array $all 
     * @param array $where 
     */ 
    public function all(){  
    //文章类
        $post = post();
        $page = post('page',1);
        $limit = post('limit',10);
        $where = '';
        $wherelist = ' a.dels<1 ';
        if($this->login['info']['ranges']<1){
            $where .= ' and a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'];
        }
        if(post('username')){
            $aid=$this->model->userinfo('username="'.post('username').'"');
            $aid =!empty($aid)?$aid:0;
            $where .= ' and a.aid ='.$aid;
        }
        if(post('acode')){
            $where .= ' and a.order_no like "%'.post('acode').'%"';
        }
        if(post('agent')){
            $where .= ' and a.pid ='.post('agent');
        }
        if(post('status')){
            $where .= ' and a.status ='.post('status');
        }
        if(post('time')){
            $time=post('time');
            switch ($time){
                case 1:
                    $where .= '';
                    break;
                case 2:
                    $where .= " and (a.createtime BETWEEN CONCAT(CURDATE(),' 00:00:00') AND CONCAT(CURDATE(),' 23:59:59')) ";
                    break;
                case 3:
                    $where .= ' and (TO_DAYS(NOW()) - TO_DAYS(a.createtime) = 1)';
                    break;
                case 4:
                    $where .= ' and (WEEK(a.createtime) = WEEK(CURDATE()))';
                    break;
                case 5:
                    $where .= " and DATE_FORMAT(a.createtime,'%Y-%m')= DATE_FORMAT(CURDATE(),'%Y-%m')";
                    break;
                case 6:
                    $where .= " and (PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(a.createtime,'%Y%m')) = 1)";
                    break;
                default:
                    return $this->echojson($this->error);
                    break;
            }         
        }
        $datalist = $this->model->list($wherelist.$where,$page,$limit);
        $wheresub =' 1=1';
        $nowdraw = $this->model->suminfo($wheresub.$where,' sum(a.money) as money, sum(a.sx_fee) as fee');
        $win = $this->model->suminfo($wheresub.$where.' and win=1',' sum(a.money) as money');
        $loss = $this->model->suminfo($wheresub.$where.' and win=2',' sum(a.money) as money');
        $nowdraw['money']=isset($nowdraw['money'])&&!empty($nowdraw['money'])?$nowdraw['money']:0;
        $nowdraw['fee']=isset($nowdraw['fee'])&&!empty($nowdraw['fee'])?$nowdraw['fee']:0;
        $nowdraw['win']=isset($win['money'])&&!empty($win['money'])?$win['money']:0;
        $nowdraw['loss']=isset($loss['money'])&&!empty($loss['money'])?$loss['money']:0;
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "sum"=> $nowdraw,
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 恢复
     * @param array $reset 
     * @param array $where 
     */
    public function reset(){ 
        $params = post();
        $info = $this->model->infolog('id='.$params['id']);
        if($info){
            $order = $this->model->orderinfo('id='.$info['orderid'],'type,rands,procode,updown,endtime,waitbuy,bond,price');
        }
        $data=[
            'id'=>$info['orderid'],//订单id
            'rid'=>$info['id'], //记录id
            'type'=>$order['type'],
            'aid'=>$info['aid'],
            'pid'=>$info['pid'],
            'proid'=>$info['proid'],
            'protype'=>$info['protype'],
            'rands'=>$order['rands'],
            'procode'=>$order['procode'],
            'updown'=>$order['updown'],
            'endtime'=>$order['endtime'],
            'status'=>$info['status'],
            'waitbuy'=>$order['waitbuy'],
            'buyprice'=>$info['buyprice'],
            'bond'=>$order['bond'],
            'point'=>json_decode($info['point'],true),
            'bondsum'=>$info['bondsum'],
            'closesum'=>$info['closesum'],
            'warnsum'=>$info['warnsum'],
            'warn'=>0,
            'price'=>$order['price'],
            'puffs'=>$info['puffs'],
            'money'=>0,
        ];
        switch ($data['waitbuy']) {
            case 1:
                $this->model->execRedis('hset',[config('tradecontrol.order'), $info['id'],json_encode($data)]);
                break;
            case 2:
                $this->model->execRedis('hset',[config('tradecontrol.ygorder'), $info['id'],json_encode($data)]);
                break;
            default:
                return $this->echojson(Lang::get('Parerror'));
                break;
        }
        $this->echoJson($this->success);
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */
    public function del(){
        $datainfo=$this->model->del(post('id',false));
        $this->echoJson($datainfo);  
    }

    /**
     * 查看
     * @param array $show 
     * @param array $where 
     */
    public function show(){ 
        $datainfo = $this->model->info(get('id'));
        if(!empty($datainfo)){
            $datainfo['orderlog']=$this->model->infologlist('orderid='.get('id'));
        }
        $this->assign('datainfo', $datainfo);
        $this->display('show.html');
    }

    /**
     * 控制
     * @param array $disk 
     * @param array $where 
     */
    public function disk(){ 
        $datainfo = $this->model->info(get('id'));
        if(!empty($datainfo)){
            $datainfo['orderlog']=$this->model->infologlist('orderid='.get('id'));
        }
        $this->assign('datainfo', $datainfo);
        $this->display('disk.html');
    }

    /**
     * 重算
     * @param array $setdisk 
     * @param array $where 
     */
    public function setdisk(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->setdisk($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success);  
                    break;
                default:
                    $this->echoJson($this->error);  
                    break;
            }
        }else{
            $datainfo = $this->model->infolog('id='.get('id'));
            // 定义变量datainfo输出            
            $this->assign('datainfo', $datainfo);
            $this->display('settle.html');
        }
    }

    /**
     * 控制
     * @param array $control 
     * @param array $where 
     */
    public function control(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->control($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success);  
                    break;
                default:
                    $this->echoJson($this->error);  
                    break;
            }
        }else{
            $params = get();
            $datainfo = $this->model->infolog('id='.$params['id']);
            $datainfo['sellprice']=$params['sellprice'];
            $datainfo['wlpoint']=$params['wlpoint'];
            $datainfo['money']=$params['money'];
            // 定义变量datainfo输出            
            $this->assign('datainfo', $datainfo);
            $this->display('control.html');
        }
    }
}
